package com.zzw.mvvm.updateutil;

import android.app.DownloadManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

/**
 * https://www.jianshu.com/p/8752d6258e4f
 */
public class UpdateApkUtil {

    private void downloadApk(Context context, String url) {
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
        request.setDescription("下载中");
        request.setTitle("我的下载");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

        }
        request.allowScanningByMediaScanner();//设置可以被扫描到
        request.setVisibleInDownloadsUi(true);// 设置下载可见
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);//下载完成后通知栏任然可见
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "my.apk");
        DownloadManager manager = (DownloadManager) context.
                getSystemService(Context.DOWNLOAD_SERVICE);
        // manager.enqueue(request);
        long Id = manager.enqueue(request);
        //listener(Id);
        SharedPreferences sPreferences = context.getSharedPreferences("downloadapk", 0);
        sPreferences.edit().putLong("apk", Id).commit();//保存此次下载ID
        Log.d("update", "开始下载任务:" + Id + " ...");
    }

}
